<template>
  <div>
    <header>路由</header>
    <div class="routes">
      <div class="item" v-for="item in routes" :key="item.path" @click="goPath(item)">
        <!-- <el-tag type="primary" size="normal" effect="dark">
          ·
        </el-tag>-->

        <div class="name">{{ item.name }}</div>
        <div class="path">{{ item.path }}</div>
      </div>
    </div>
    <!-- <pdf src="https://kano.guahao.cn/USq554374379"></pdf> -->
    <div class="select_invoice">
      <!--  <el-upload
        class="upload-demo"
        :multiple="true"
        action="https://jsonplaceholder.typicode.com/posts/"
        :data="uploadData"
        accept="bmg, .png, .jpg, .jpeg"
        list-type="picture-card"
        :before-upload="beforeUpload"
        :on-success="handleSuccess"
        :on-error="hanldeError"
        multiple
        :limit="3"
      >
        <el-button size="small" type="primary">添加图片</el-button>
      </el-upload>-->
    </div>
    <el-form>
      <div>
        <el-form-item label="前缀">
          <el-select v-model="perfix">
            <el-option v-for="item in perfixList" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item label="是否纯个账">
          <el-select v-model="miAccountPayMark" placeholder="是否纯个账">
            <el-option :value="1" label="是">是</el-option>
            <el-option :value="0" label="否">否</el-option>
          </el-select>
        </el-form-item>
      </div>
    </el-form>

    <el-divider></el-divider>
    <div class="pay">
      <el-input id="input" v-model="inputData" style="width: 500px" clearable></el-input>
      <el-button @click="pay($event)">mh5付款</el-button>
      <el-button @click="refund($event)">mh5退款</el-button>
      <el-button @click="refundNewUrl">mh5退款银联</el-button>
      <el-button @click="pay($event, 'mh3')">mh3付款</el-button>
      <el-button @click="refund($event, 'mh3')">mh3退款</el-button>
      <el-divider></el-divider>
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="签名">
          <el-input v-model="form.sign" placeholder="签名" clearable></el-input>
        </el-form-item>
        <el-select v-model="currentPerson" value-key="idNo">
          <el-option v-for="item in personInfo" :label="item.name" :value="item" :key="item.idNo"></el-option>
        </el-select>
      </el-form>
      <el-button @click="upload" :loading="globalLoading">上传处方并下单mh3</el-button>
      <el-button @click="uploadMh5" :loading="globalLoading">上传处方并下单mh5</el-button>
    </div>
  </div>
</template>
<script>
import route, { showNotification } from "../router/index"
import pdf from "vue-pdf"
import { log } from "../utils/decorator"
import Clipboard from "../utils/clipboard/index"
// import fetch from "@/utils/axios"
import dayjs from "dayjs"
import Qs from "qs"
import { loading } from "@/utils/axios"
export default {
  name: "pageIndex",
  directives: {
    Clipboard,
  },
  components: {
    pdf,
  },
  data() {
    return {
      perfix: 'http://localhost:8080',
      perfixList: [
        {
          name: 'qa1',
          value: 'http://local-pay-static-qa1.guahao-test.com'
        },
        {
          name: 'qa2',
          value: 'http://local-pay-static-qa2.guahao-test.com'
        },
        {
          name: 'qa3',
          value: 'http://local-pay-static-qa3.guahao-test.com'
        },
        {
          name: 'local',
          value: 'http://localhost:8080'
        }
      ],
      globalLoading: loading,
      routes: "",
      uploadPath: "",
      inputData: "",
      //  上传处方
      form: {
        sign: "fdd5485fb113994b8ee99981d9dbc793",
      },
      miAccountPayMark: 0,
      // 处方编号
      prescriptionSerialNo: "",
      currentPerson: null,
      // 人员信息
      personInfo: [{
        idNo: "330325196811144217",
        name: '张洪猛',
        miNo: 'TEST26017950',
        miCenterNo: 379900,
        hospitalNo: "5990336"
      }, {
        idNo: "360302198908235318",
        name: '王涛',
        miNo: 'TEST26017950',
        // miCenterNo: 370100,
        miCenterNo: '379900',
        hospitalNo: "015026"
      }]
    }
  },
  mounted() {
    this.routes = route.options.routes
    this.currentPerson = this.personInfo[1]
  },
  computed: {
    currentPath() {
      console.log("this.$router", this.$router)
      return this.$router.currentRoute.path
    },
    payUrl() {
      return `${this.perfix}/djk/orderPayUnionpay?prePayId=${this.inputData}`
    },
    refundUrl() {
      return `${this.perfix}/djk/refund?preRefundId=${this.inputData}`
    },
    refundUrlUni() {
      return `${this.perfix}/djk/refundUnion?preRefundId=${this.inputData}`
    },
    payUrlMh3() {
      return `${this.perfix}/djk/mh3/pay?prePayId=${this.inputData}`
    },
    refundUrlMh3() {
      return `${this.perfix}/djk/mh3/refund?preRefundId=${this.inputData}`
    },
  },
  methods: {
    // 上传处方
    upload() {
      const temp = { "adress": "", "complain": "慢病取药", "diseaseName": "冠状动脉粥样硬化性心脏病", "diseaseNo": "I25.103", "doctorName": "皇甫丰田", "doctorNo": "D370105008105", "expireDate": "20210916090636", "hospNo": "015026", "idNo": "370111197007125253", "idType": "1", "itemList": [{ "commonName": "银杏叶滴丸", "deptNo": "008", "dosage": "丸", "dosageNum": "5.0", "hospItemNo": "100192", "itemName": "银杏叶滴丸", "itemNo": "XA12BAM074A001010179164", "manufacturer": "万邦德制药集团股份有限公司", "phr": "3", "price": "4077.0000", "selfRate": "0", "seqNo": "1", "spec": "63mg（银杏叶提取物18mg）*5丸*20袋", "totalTakeNum": "3", "totalUnit": "盒", "unit": "丸", "usage": "口服" }, { "commonName": "血脂康胶囊", "deptNo": "2008", "dosage": "粒", "dosageNum": "0.6", "hospItemNo": "100155", "itemName": "血脂康胶囊", "itemNo": "ZA17AAX0881010100009", "manufacturer": "北京北大维信生物科技有限公司", "phr": "5", "price": "1480.0000", "selfRate": "0", "seqNo": "2", "spec": "0.3g*12粒", "totalTakeNum": "5", "totalUnit": "盒", "unit": "粒", "usage": "口服" }, { "commonName": "酒石酸美托洛尔", "deptNo": "2008", "dosage": "片", "dosageNum": "1.0", "hospItemNo": "104536", "itemName": "酒石酸美托洛尔", "itemNo": "XR03AKS054L030020178564", "manufacturer": "石家庄以岭药业股份有限公司", "phr": "2", "price": "177.0000", "selfRate": "0.05", "seqNo": "3", "spec": "25mg*30片", "totalTakeNum": "2", "totalUnit": "盒", "unit": "片", "usage": "口服" }, { "commonName": "介宁", "deptNo": "2008", "dosage": "片", "dosageNum": "2.0", "hospItemNo": "100321", "itemName": "阿司匹林肠溶缓释片", "itemNo": "XB01ACA056A023010104187", "manufacturer": "山东新华制药股份有限公司", "phr": "2", "price": "1380.0000", "selfRate": "0.05", "seqNo": "4", "spec": "50mg*12片*2板", "totalTakeNum": "2", "totalUnit": "盒", "unit": "片", "usage": "口服" }], "miCenterNo": this.currentPerson.miCenterNo, "miNo": "O22544476", "miType": 5, "mobile": "", "name": "李峰", "takeType": "1", "visitNo": "2021091509063689" }
      var formdata = new FormData()
      formdata.append("time", "20200802")
      formdata.append("sign", this.form.sign)
      formdata.append("path", "com.wedoctor.pay.paylocal.biz.manager.mi.impl.MiExchangeManagerImpl")
      formdata.append("method", "submitPrescription")
      formdata.append(
        "param",
        JSON.stringify(temp)
      )

      var requestOptions = {
        method: "POST",
        body: formdata,
        redirect: "follow",
      }
      console.log(formdata)
      console.log(JSON.stringify(requestOptions))
      fetch("/api/remote_invoke_test/call_method", requestOptions)
        .then((response) => response.text())
        .then((result) => {
          const resObj = JSON.parse(result)
          if (+resObj.code !== 0) return showNotification("请求结果中的报错", resObj.message)
          this.prescriptionSerialNo = resObj.data
          console.log(this.prescriptionSerialNo)
          // 下订单
          this.placeOrder()
        })
        .catch((error) => showNotification("请求结果中的报错", error.message, 10000, "top-left", "error"))
    },
    // 下单mh3
    placeOrder() {
      var formdata = new FormData()
      let params = {
        amount: 44935,
        bizCategory: "biz_mbyf",
        bizOrderNo: new Date().getTime().toString(),
        body: "济南第四人民医院慢病药房门诊订单",
        clientIp: "127.0.0.1",
        expiredTime: dayjs().add(60, 'm').format("YYYYMMDDHHmmss"),
        journalNo: "N81609698557",
        miSettle: {
          certificateNo: "",
          homePayMark: 0,
          hospitalNo: "015026",
          idNo: "370111197007125253",
          medicalPlanType: 4,
          // miCenterNo: "370100",
          miCenterNo: "379900",
          miNo: "O22544476",
          miType: 5, // 5 是dll  1是webservice
          mobile: "",
          name: "李峰",
          prescriptionSerialNo: this.prescriptionSerialNo,
          miAccountPayMark: this.miAccountPayMark,
        },
        notifyUrl: "http://10.200.111.81:8888/api/local/bill/payCallBack",
        platId: "merchant_pay20180319",
        returnUrl: "https://doctorstation.95169000.com/charge/chargeSettlementMh?visitId=c7639180-1b50-4196-a081-16dcbdb2c01a&prescriptionId=2184245819275113893&orderType=1&orderNo=ed5228414b274422",
        source: "web",
        timeStamp: "20210915090636",
        title: "济南第四人民医院慢病药房门诊订单",
        userId: "0",
        version: "1.0",
      }
      formdata.append("time", "20200801")
      formdata.append("sign", this.form.sign)
      formdata.append("path", "com.wedoctor.pay.paylocal.biz.manager.pay.impl.PayServiceManagerImpl")
      formdata.append("method", "orderSubmit")
      formdata.append("param", JSON.stringify(params))
      var requestOptions = {
        method: "POST",
        body: formdata,
        redirect: "follow",
      }

      fetch("/api/remote_invoke_test/call_method", requestOptions)
        .then((response) => response.text())
        .then((result) => {
          const resObj = JSON.parse(result)
          if (!resObj.data.prePayId) return showNotification("请求结果中的报错", resObj.message, 10000, "top-left", "error")
          this.inputData = resObj.data.prePayId
        })
        .catch((error) => {
          showNotification("请求结果中的报错", error.message, 10000, "top-left", "error")
        })
    },



    //
    uploadMh5() {
      let obj = { "adress": "", "complain": "慢病取药", "diseaseName": "冠状动脉粥样硬化性心脏病", "diseaseNo": "I25.103", "doctorName": "皇甫丰田", "doctorNo": "D370105008105", "expireDate": "20210916090636", "hospNo": this.currentPerson.hospitalNo, "idNo": this.currentPerson.idNo, "idType": "1", "itemList": [{ "commonName": "银杏叶滴丸", "deptNo": "2008", "dosage": "丸", "dosageNum": "5.0", "hospItemNo": "100192", "itemName": "银杏叶滴丸", "itemNo": "XA12BAM074A001010179164", "manufacturer": "万邦德制药集团股份有限公司", "phr": "3", "price": "42.96", "selfRate": "0", "seqNo": "1", "spec": "63mg（银杏叶提取物18mg）*5丸*20袋", "totalTakeNum": "1", "totalUnit": "盒", "unit": "丸", "usage": "口服" }], "miCenterNo": this.currentPerson.miCenterNo, "miNo": this.currentPerson.miNo, "miType": 1, "mobile": "", "name": this.currentPerson.name, "takeType": "1", "visitNo": "2021091509063689" }
      // 上传处方
      var formdata = new FormData()
      formdata.append("time", "20200802")
      formdata.append("sign", this.form.sign)
      formdata.append("path", "com.wedoctor.pay.paylocal.biz.manager.mi.impl.MiExchangeManagerImpl")
      formdata.append("method", "submitPrescription")
      formdata.append(
        "param",
        JSON.stringify(obj)
      )

      var requestOptions = {
        method: "POST",
        body: formdata,
        redirect: "follow",
      }

      fetch("/api/remote_invoke_test/call_method", requestOptions)
        .then((response) => response.text())
        .then((result) => {
          const resObj = JSON.parse(result)
          if (+resObj.code !== 0) return showNotification("请求结果中的报错", resObj.message)
          this.prescriptionSerialNo = resObj.data

          console.log(this.prescriptionSerialNo)
          // 下订单
          this.placeOrderMh5()
        })
        .catch((error) => showNotification("请求结果中的报错", error.message, 10000, "top-left", "error"))
    },
    placeOrderMh5() {
      var formdata = new FormData()
      let params = {
        amount: 4296,
        bizCategory: "biz_mbyf",
        bizOrderNo: new Date().getTime().toString(),
        body: "济南第四人民医院慢病药房门诊订单",
        clientIp: "127.0.0.1",
        // expiredTime: "20211231093036",
        expiredTime: dayjs().add(600, 'm').format("YYYYMMDDHHmmss"),
        journalNo: "N81609698557",
        miSettle: {
          certificateNo: "",
          homePayMark: 0,
          hospitalNo: this.currentPerson.hospitalNo,
          idNo: this.currentPerson.idNo, // todo 身份证号
          medicalPlanType: 4,
          miCenterNo: this.currentPerson.miCenterNo, // todo 医保省市编码
          miNo: this.currentPerson.miNo, // todo 卡号
          miType: 1, //  todo 5 是dll  1是webservice
          mobile: "",
          name: this.currentPerson.name, // todo 姓名
          prescriptionSerialNo: this.prescriptionSerialNo,
        },
        notifyUrl: "http://10.200.111.81:8888/api/local/bill/payCallBack",
        platId: "merchant_pay20180319",
        returnUrl: "https://doctorstation.95169000.com/charge/chargeSettlementMh?visitId=c7639180-1b50-4196-a081-16dcbdb2c01a&prescriptionId=2184245819275113893&orderType=1&orderNo=ed5228414b274422",
        source: "web",
        timeStamp: "20210915090636",
        title: "济南第四人民医院慢病药房门诊订单",
        userId: "0",
        version: "1.0",
      }
      formdata.append("time", "20200801")
      formdata.append("sign", this.form.sign)
      formdata.append("path", "com.wedoctor.pay.paylocal.biz.manager.pay.impl.PayServiceManagerImpl")
      formdata.append("method", "orderSubmit")
      formdata.append("param", JSON.stringify(params))
      var requestOptions = {
        method: "POST",
        body: formdata,
        redirect: "follow",
      }

      fetch("/api/remote_invoke_test/call_method", requestOptions)
        .then((response) => response.text())
        .then((result) => {
          const resObj = JSON.parse(result)
          if (!resObj.data.prePayId) return showNotification("请求结果中的报错", resObj.message, 10000, "top-left", "error")
          this.inputData = resObj.data.prePayId
        })
        .catch((error) => {
          showNotification("请求结果中的报错", error.message, 10000, "top-left", "error")
        })
    },
    // @log()
    pay(event, mh3) {
      this.$message({
        message: "Copy successfully",
        type: "success",
        duration: 1500,
      })
      console.log("mh3?", mh3 ? true : false)
      console.log("mh3?", mh3)
      window.open(mh3 ? this.payUrlMh3 : this.payUrl, "target")
    },
    refund(event, mh3) {
      this.$message({
        message: "Copy successfully",
        type: "success",
        duration: 1500,
      })
      window.open(mh3 ? this.refundUrlMh3 : this.refundUrl, "target")
    },
    refundNewUrl() {
      window.open(this.refundUrlUni, "target")
    },
    goPath(item) {
      if (item.path == this.currentPath) return
      this.$router.push({ path: item.path })
    },
    // 第一种,不考虑图片长宽,只考虑图片大小的情况,图片超过4M就压缩
    beforeUpload(file) {
      return new Promise((resolve, reject) => {
        let isLt2M = file.size / 1024 / 1024 < 4 // 判定图片大小是否小于4MB
        if (isLt2M) {
          resolve(file)
        }
        console.log(file)
        // 压缩到400KB,这里的400就是要压缩的大小,可自定义
        imageConversion.compressAccurately(file, 400).then((res) => {
          console.log({ res })
          resolve(res)
        })
      })
    },
  }
}
</script>
<style lang="less" scoped>
.item {
  display: flex;
  align-items: center;
  margin: 15px;
  cursor: pointer;
  .path {
    margin-left: 10px;
  }
}
.pay {
  padding: 20px;
}
</style>
